package net.ycxinxing.mcpebox.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
import net.ycxinxing.mcpebox.Pro.R;

public class mMessageToast
{

	private boolean mIsShow;

	private boolean mShowTime;

	private WindowManager mWdm;

	private View mToastView;

	private Timer mTimer;

	private WindowManager.LayoutParams mParams;
	
	public mMessageToast(Context context, String text, boolean showTime ){
		mShowTime = showTime;//记录Toast的显示长短类型
		mIsShow = false;//记录当前Toast的内容是否已经在显示
		mWdm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		//通过Toast实例获取当前android系统的默认Toast的View布局
		mToastView = Toast.makeText(context, text, Toast.LENGTH_SHORT).getView();
		mToastView.setBackgroundColor(Color.BLACK);
		mTimer = new Timer();
		//设置布局参数
		setParams();
	}
	public static mMessageToast MakeText(Context context, String text, boolean showTime) {
		mMessageToast result = new mMessageToast(context, text, showTime);
		return result;
	}
	private void setParams() {
		mParams = new WindowManager.LayoutParams();
		mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;  
		mParams.width = WindowManager.LayoutParams.MATCH_PARENT;
		mParams.format = PixelFormat.TRANSLUCENT;  
		mParams.windowAnimations = R.style.anim_view;//设置进入退出动画效果
		mParams.type = WindowManager.LayoutParams.TYPE_TOAST;  
		mParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON  
			| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  
			| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
		mParams.gravity = Gravity.BOTTOM;
		//mParams.y = 250;
	}
	public void show(){
		if(!mIsShow){//如果Toast没有显示，则开始加载显示
			mIsShow = true;
			mWdm.addView(mToastView, mParams);//将其加载到windowManager上
			mTimer.schedule(new TimerTask() {
					@Override
					public void run() {
						mWdm.removeView(mToastView);
						mIsShow = false;
					}
				}, (long)(mShowTime ? 3500 : 2000));
		}
	}
}
